package great.project.advice.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.lang.NonNull;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice;

import java.io.IOException;
import java.lang.reflect.Parameter;
import java.lang.reflect.Type;

/**
 * <div  style="border:2px solid none;margin:20px;padding:15px;font-size:24px;text-align:center;color:aqua;">
 *
 *
 *
 *  </div>
 *
 * @author v_zhangshun01
 * @date 2021/11/25 4:02 下午
 */
@Slf4j
@ControllerAdvice
public class InputAdvice implements RequestBodyAdvice {

    @Override
    public boolean supports(@NonNull MethodParameter methodParameter, @NonNull Type targetType, @NonNull Class<? extends HttpMessageConverter<?>> converterType) {
        System.out.println(methodParameter);
        System.out.println(targetType);
        System.out.println(converterType);
        System.out.println(targetType.getTypeName());
        System.out.println(converterType.getTypeName());
        final Parameter parameter = methodParameter.getParameter();
        System.out.println(parameter.getName());
        return true;
    }

    @NonNull
    @Override
    public HttpInputMessage beforeBodyRead(
            @NonNull HttpInputMessage inputMessage,
            @NonNull MethodParameter parameter,
            @NonNull Type targetType,
            @NonNull Class<? extends HttpMessageConverter<?>> converterType
    ) throws IOException {
        System.out.println(inputMessage);
        System.out.println(parameter);
        System.out.println(targetType);
        System.out.println(converterType);
        return inputMessage;
    }

    @NonNull
    @Override
    public Object afterBodyRead(
            @NonNull Object body,
            @NonNull HttpInputMessage inputMessage,
            @NonNull MethodParameter parameter,
            @NonNull Type targetType,
            @NonNull Class<? extends HttpMessageConverter<?>> converterType
    ) {
        System.out.println(body);
        System.out.println(inputMessage);
        System.out.println(parameter);
        System.out.println(targetType);
        System.out.println(converterType);

        return body;
    }

    @Override
    public Object handleEmptyBody(
            Object body,
            @NonNull HttpInputMessage inputMessage,
            @NonNull MethodParameter parameter,
            @NonNull Type targetType,
            @NonNull Class<? extends HttpMessageConverter<?>> converterType
    ) {
        System.out.println(body);
        System.out.println(inputMessage);
        System.out.println(parameter);
        System.out.println(targetType);
        System.out.println(converterType);

        return body;
    }
}
